package com.xaicode.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.xaicode.auth.annotation.Log;
import com.xaicode.auth.domain.Result;
import com.xaicode.auth.domain.params.IdParam;
import com.xaicode.auth.domain.params.IdsParam;
import com.xaicode.auth.entity.SysRole;
import com.xaicode.auth.enums.BusinessType;
import com.xaicode.auth.security.domain.dto.SysRoleDto;
import com.xaicode.auth.security.domain.params.SysRoleQueryParam;
import com.xaicode.auth.service.ISysRoleService;
import com.xaicode.auth.utils.GlobalValidator;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色管理接口
 * 
 * @date 2024/12/20
 */
@RestController
@RequestMapping("/api/system/role")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;


    @Log(title = "查看角色列表", businessType = BusinessType.QUERY, isSaveResponseData = false)
    @SaCheckPermission(value = {"system:role:list", "system:user:list"}, mode = SaMode.OR)
    @PostMapping("/page")
    public Result<?> page(@RequestBody SysRoleQueryParam param) {
        PageInfo<SysRole> pageInfo = sysRoleService.queryPage(param);
        return Result.ok(pageInfo);
    }

    @Log(title = "新增角色", businessType = BusinessType.INSERT)
    @SaCheckPermission("system:role:add")
    @PostMapping("/create")
    public Result<?> create(@Validated @RequestBody SysRoleDto entity) {
        sysRoleService.createRole(entity);
        return Result.ok();
    }

    @Log(title = "修改角色", businessType = BusinessType.UPDATE)
    @SaCheckPermission("system:role:edit")
    @PostMapping("/update")
    public Result<?> update(@Validated @RequestBody SysRoleDto entity) {
        GlobalValidator.notNull(entity.getId(), "id不能为空");
        sysRoleService.updateRole(entity);
        return Result.ok();
    }

    @Log(title = "删除角色", businessType = BusinessType.DELETE)
    @SaCheckPermission("system:role:del")
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody IdsParam<Integer> ids) {
        List<Integer> idList = ids.getIds();
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }

    /**
     * 获取指定角色关联的菜单id集合
     *
     * @param idParam 角色id
     * @return 菜单id集合
     */
    @SaCheckPermission("system:role:list")
    @PostMapping("/menus")
    public Result<?> getRoleMenus(@RequestBody IdParam<Integer> idParam) {
        Integer roleId = idParam.getId();
        List<Integer> menuIds = sysRoleService.getMenuIdsByRoleId(roleId);
        return Result.ok(menuIds);
    }

    /**
     * 获取指定角色关联的部门id集合
     *
     * @param idParam 角色id
     * @return 部门id集合
     */
    @SaCheckPermission("system:role:list")
    @PostMapping("/depts")
    public Result<?> getRoleDepts(@RequestBody IdParam<Integer> idParam) {
        Integer roleId = idParam.getId();
        List<Integer> deptIds = sysRoleService.getDeptIdsByRoleId(roleId);
        return Result.ok(deptIds);
    }

}
